#pragma once
#include <iostream>
#include <string>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <memory>
#include "Common.hpp"
#include "InetAddr.hpp"
#include "Log.hpp"
using namespace my_log;
static const int gbacklog = 8;//允许8个客户端连接
namespace SocketMoudule
{
    class Socket
    {
    public:
        virtual void SocketOrDie() = 0;
        virtual void BindOrDie(uint16_t) = 0;
        virtual void ListenOrDie(int) = 0;
        virtual shared_ptr<Socket> Accept(InetAddr *) = 0;
        virtual void Close() = 0;
        virtual int Recv(string *) = 0;
        virtual int Send(string &) = 0;
        virtual void Connect(const std::string &, uint16_t) = 0;
        virtual int Fd() = 0;

    public:
        void BuildTcpServerMoudule(uint16_t port, int backlog = gbacklog)
        {
            SocketOrDie();
            BindOrDie(port);
            ListenOrDie(backlog);
        }
        void BuildUdpServerMoudule()
        {
            void SocketOrDie();
            void BindOrDie();
        }
    };
    class TcpSocket : public Socket
    {
    public:
        TcpSocket(int socketfd = -1)
            : _socketfd(socketfd)
        {
        }
        virtual void SocketOrDie() override
        {
            _socketfd = socket(AF_INET, SOCK_STREAM, 0);
            if (_socketfd < 0)
            {
                LOG(Level::FATAL) << "Socket fail";
                exit(SOCKET_ERRO);
            }
            LOG(Level::INFO) << "socket success";
        }
        virtual void BindOrDie(uint16_t port) override
        {
            InetAddr addr(port);
            int n = bind(_socketfd, addr.getaddr(), addr.getlen());
            if (n < 0)
            {
                LOG(Level::FATAL) << "bind fail";
                exit(BIND_ERRO);
            }
            LOG(Level::INFO) << "Bind success";
        }
        virtual void ListenOrDie(int backlog) override
        {
            int n = listen(_socketfd, backlog);
            if (n < 0)
            {
                LOG(Level::FATAL) << "Listen fail";
                exit(LISTEN_ERRO);
            }
            LOG(Level::INFO) << "Listen success";
        }
        virtual shared_ptr<Socket> Accept(InetAddr *addr) override
        {
            // 为什么不直接用addr，因为构造不了IP。
            socklen_t len = sizeof(sockaddr_in);
            sockaddr_in peer;
            int n = accept(_socketfd, (sockaddr *)&peer, &len);
            if (n < 0)
            {
                LOG(Level::WARING) << addr->tostring_ip() << "accept fail";
                return nullptr;
            }
            *addr = InetAddr(peer);
            return make_shared<TcpSocket>(n);
        }
        virtual void Close() override
        {
            close(_socketfd);
        }
        virtual int Recv(string *out) override
        {
            char buffer[1024];
            int n = read(_socketfd, buffer, sizeof(buffer) - 1);
            if (n > 0)
            {
                buffer[n] = '\0';
                *out += buffer;
            }
            return n;
        }
        virtual int Send(string &message) override
        {
            return write(_socketfd, message.c_str(), message.size());
        }
        virtual void Connect(const std::string &ip, uint16_t port) override
        {
            InetAddr addr(port, ip);
            int n = connect(_socketfd, addr.getaddr(), addr.getlen());
            if (n < 0)
            {
                LOG(Level::FATAL) << "connect fail";
                exit(CONNECT_ERRO);
            }
            LOG(Level::INFO) << "connect success";
        }
        virtual int Fd()
        {
            return _socketfd;
        }
    private:
        int _socketfd;
    };
    ////////////////////////////////////////////////////////////
    class UdpSocket : public Socket
    {
    public:
        void SocketOrDie()
        {
            _socketfd = socket(AF_INET, SOCK_STREAM, 0);
            if (_socketfd < 0)
            {
                LOG(Level::FATAL) << "socket fail";
                exit(SOCKET_ERRO);
            }
        }
        //......
    private:
        int _socketfd;
    };
}

